const axios = require('axios');
var express = require('express');
var router = express.Router();
const upload = require('../../utils/update');
const { success, fail } = require('../../utils/response');

router.post('/upload', upload.single('file'), function(req, res, next) {
  try{
    success(res, 'http://localhost:3366/' + req.file.path, req.t('success'));
  }catch(err){
    fail(res, err, req.t('fail'));
  }
});

router.post('/translate', async function(req, res, next){
  const { text, from='zh', to='en' } = req.body;
  try {
    if(!text){
      throw new Error("传入text值为空");
    }
    const response = await axios.post(
      'http://api.niutrans.com/NiuTransServer/translation', 
      { src_text: text, from, to, apikey: '69270649936f71c7eb3d7d32aa7ab178'}
    );
    success(res, response.data, req.t('success'));
  } catch (error) {
    fail(res, error, req.t('fail'));
  }
});

module.exports = router;
